/**
 * Created by apple on 16/3/8.
 */
class Service {
    constructor(CommonService) {
        "ngInject"

        this.CommonService = CommonService;
        this.uriMap = {
            login          : "/login",
            user           : "/system_user",
            role           : "/system_roles",
            postRoleInsert : "/system_post_role/batch/one2ManyInsert",
            postRoleDelete : "/system_post_role/batch/delete",
            userRoleInsert : "/system_user_role/batch/one2ManyInsert",
            userRoleDelete : "/system_user_role/batch/delete",
            userPostInsert : "/system_user_post/batch/one2ManyInsert",
            userPostDelete : "/system_user_post/batch/delete",
            post           : "/system_post",
            post2Roles     : '/system_roles/page/post2Roles',
            user2Roles     : '/system_roles/page/user2Roles',
            postByUser     : "/system_post/query/sysUserId",
            staff          : "/staff",
            org            : "/organization",
            auth           : "/privilege",
            sub            : "/organization/sub",
            menuComp       : "/func_menu/sub_menus",
            menuTree       : "/func_menu/sub_menu",
            privGrant      : "/func_comp/priv_grant_query",
            comp           : "/func_comp",
            batchPriv      : "/priv_grant/batch",
            menu           : "/func_menu",
            menuUser       : "/perm/menu_tree",
            userInfo       : "/permission/user",
            regionTree     : "/common_region/sub",
            region         : "/common_region",
            plat           : "/plat_info",
            pwd            : "/system_user/reset/password",
            permission     : "/permission/func_comp",
            cookie         : "/cookie/ctgUser",
            updatePwd      : "/system_user/update/password",
            pwdStrategy    : "/password_rule/rules",
            resetPwd       : "/system_user/api/password"
        }
        this.page = {
            perPage : 8
        }
    }

    getUserInfo() {
        return this.CommonService.request({
            url    : this.uriMap['userInfo']
        })
    }

    getMenuUser() {
        return this.CommonService.request({
            url    : this.uriMap['menuUser'],
            data : {
                sysCode : 1
            }
        })
    }

    login(params) {
        params = params || {};
        return this.CommonService.request({
            method : "POST",
            url    : this.uriMap['login'],
            data   : params
        })
    }

    getPostByUser(params) {
        params = params || {};
        return this.CommonService.request({
            method : "GET",
            url    : this.uriMap['postByUser'],
            data : params
        })
    }

    updatePassword(params) {
        params = params || {};
        return this.CommonService.request({
            method : "POST",
            url    : this.uriMap['updatePwd'],
            data   : params
        })
    }

    getItem(type, params) {
        params = params || {};
        return this.CommonService.request({
            url    : this.uriMap[type] + '/get',
            data : params
        })
    }

    getComp(type, params = {}) {
        return this.CommonService.request({
            url    : this.uriMap[type],
            data : params
        })
    }

    getList(type, params, page) {
        params = $.extend({}, params, page || this.page);
        return this.CommonService.request({
            url    : this.uriMap[type] + '/page' + (type == 'post' ? '/orgId' : ''),
            data : params
        })
    }

    searchRole(params = {}) {
        return this.CommonService.request({
            method : "POST",
            url    : this.uriMap['role'] + '/page',
            data   : params
        })
    }

    delItem(type, params) {
        params = params || {};
        return this.CommonService.request({
            method : "GET",
            url    : this.uriMap[type] + '/delete',
            data : params
        })
    }

    update(type, params = {}) {
        return this.CommonService.request({
            method : "POST",
            url    : this.uriMap[type] + (type == 'plat'? '/edit' : '/update'),
            data   : params
        });
    }

    insert(type, params = {}) {
        return this.CommonService.request({
            method : "POST",
            url    : this.uriMap[type] + (type == 'plat'? '/create' : '/insert'),
            data   : params
        });
    }

    post2Roles(params = {}) {
        return this.CommonService.request({
            method : "POST",
            url    : this.uriMap.post2Roles,
            data   : params
        });
    }

    user2Roles(params) {
        params = params || {}
        return this.CommonService.request({
            url    : this.uriMap.user2Roles,
            data : params
        });
    }

    postInsert(params = {}) {
        return this.CommonService.request({
            method : "POST",
            url    : this.uriMap.postInsert,
            data   : params
        })
    }

    postRoleInsert(params) {
        return this.CommonService.request({
            method : "POST",
            url    : this.uriMap.postRoleInsert,
            data   : params
        })
    }

    userRoleInsert(params) {
        return this.CommonService.request({
            method : "POST",
            url    : this.uriMap.userRoleInsert,
            data   : params
        })
    }

    userPostInsert(params) {
        return this.CommonService.request({
            method : "POST",
            url    : this.uriMap.userPostInsert,
            data   : params
        })
    }

    postRoleDelete(params) {
        return this.CommonService.request({
            method : "POST",
            url    : this.uriMap.postRoleDelete,
            data   : params
        })
    }

    userRoleDelete(params) {
        return this.CommonService.request({
            method : "POST",
            url    : this.uriMap.userRoleDelete,
            data   : params
        })
    }

    userPostDelete(params) {
        return this.CommonService.request({
            method : "POST",
            url    : this.uriMap.userPostDelete,
            data   : params
        })
    }

    getTree(type, params) {
        if (!params) {
            params = type;
            type   = 'sub'
        }
        return this.CommonService.request({
            url    : this.uriMap[type],
            data : params
        })
    }

    generate(type, params) {
        return this.CommonService.request({
            url    : this.uriMap[type],
            data : params
        })
    }

    permission(params) {
        return this.CommonService.request({
            url    : this.uriMap['permission'],
            data : params
        })
    }

    getPlat(type) {
        console.log(1)
        return this.CommonService.request({
            url : this.uriMap['plat'] + (type == 'plat' ?  '/page' :  '/query')
        })
    }

    getRegion(params = {}, edit = false) {
        return this.CommonService.request({
            url    : this.uriMap['region'] + (!edit ?  '/query' : '/query/editData'),
            data : params
        })
    }

    getRegionTree(params = {}) {
        return this.CommonService.request({
            url    : this.uriMap['regionTree'],
            data : params
        })
    }

    /**
     * 检查组织是否同名
     * @param params {orgName|regionName, parentOrgId}
     * @returns {*}
     */
    checkExist(type, params = {}) {
        return this.CommonService.request({
            method : "POST",
            url    : this.uriMap[type] + '/exist',
            data   : params
        })
    }

    setCookie() {
        return this.CommonService.request({
            url : this.uriMap['cookie']
        })
    }

    pwdStrategy(method, params = {}) {
        return this.CommonService.request({
            method : method,
            url    : this.uriMap['pwdStrategy'],
            data   : params
        })
    }

    getPwdRule(type, params) {
        return this.CommonService.request({
            url    : this.uriMap['pwdStrategy'] + '/' + type,
            data   : params
        })
    }

    resetPwd(params) {
        return this.CommonService.request({
            method : "post",
            url    : this.uriMap['resetPwd'],
            data   : params
        })
    }

    static factory(CommonService) {
        "ngInject"
        return new Service(CommonService)
    }
}
export default Service;

